/**
 * 
 */
package com.wodsy.phoenix.datadao.cms.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import com.wodsy.phoenix.datadao.base.BaseHibernateDao;
import com.wodsy.phoenix.datadao.cms.dao.CmsHomePageNewGoodsDao;
import com.wodsy.phoenix.dataobject.base.Pager;
import com.wodsy.phoenix.dataobject.cms.constants.CmsContentStatus;
import com.wodsy.phoenix.dataobject.cms.model.IdxNewGoodsRecommended;

/**
 * @author bryan
 *
 */
@Repository("cmsHomePageNewGoodsDaoImpl")
public class CmsHomePageNewGoodsDaoImpl extends BaseHibernateDao<IdxNewGoodsRecommended, Integer>
		implements CmsHomePageNewGoodsDao {
	
    @Autowired
    @Qualifier(value="cms")
	@Override
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	@Override
	public Class<IdxNewGoodsRecommended> getModelClass() {
		return IdxNewGoodsRecommended.class;
	}
	
	@Override
	public List<IdxNewGoodsRecommended> getOnlineNewGoodsRec() {
		return getSession().createCriteria(getModelClass())
				.add(Restrictions.eq("status", CmsContentStatus.ISONLINE.getKey()))
				.addOrder(Property.forName("weight").asc())
				.setMaxResults(6)
				.list();
	}
	
	@Override
	public Pager<IdxNewGoodsRecommended> searchNewGoodsRecByOrder(
			Pager<IdxNewGoodsRecommended> newGoodsRec,
			IdxNewGoodsRecommended goodsreq) {
		Criteria criteria = getSession().createCriteria(getModelClass());
		if(!StringUtils.isEmpty(goodsreq.getGoodsName())){
			criteria.add(Restrictions.like("goodsName", "%"+goodsreq.getGoodsName()+"%"));
	    }
		if(!StringUtils.isEmpty(goodsreq.getStatus())) {
			criteria.add(Restrictions.eq("status", goodsreq.getStatus()));
		}
		if(!StringUtils.isEmpty(goodsreq.getOnlineTime())&&!StringUtils.isEmpty(goodsreq.getOfflineTime())) {
			criteria.add(Restrictions.le("onlineTime", goodsreq.getOnlineTime()));
			criteria.add(Restrictions.ge("offlineTime", goodsreq.getOfflineTime()));
		}else if(!StringUtils.isEmpty(goodsreq.getOnlineTime())){
			criteria.add(Restrictions.le("onlineTime", goodsreq.getOnlineTime()));
			criteria.add(Restrictions.ge("offlineTime", goodsreq.getOnlineTime()));
		}
        criteria.addOrder(Property.forName("status").asc()).addOrder(Property.forName("weight").asc());
        return toPager(newGoodsRec, criteria);
	}
	@Override
	public Integer updateGoodsRecommendedStatus(int goods_recId, int status) {
		Session session = getSession();
		String hqlUpdate =
				"update IdxNewGoodsRecommended t set t.status = :status,t.updateTime= :updateTime where t.id = :goods_recId";
		int updatedEntities = session.createQuery( hqlUpdate )
			.setInteger("status", status)
			.setTimestamp("updateTime", new Date())
			.setInteger("goods_recId", goods_recId)
			.executeUpdate();
		return updatedEntities;
	}
	
	@Override
	public Integer updateGoodsRecommendedStatus(Integer[] goods_recIds, int status) {
		Session session = getSession();
		String hqlUpdate =
				"update IdxNewGoodsRecommended t set t.status = :status,t.updateTime= :updateTime where t.id in (:goods_recIds)";
		int updatedEntities = session.createQuery( hqlUpdate )
			.setInteger("status", status)
			.setTimestamp("updateTime", new Date())
			.setParameterList("goods_recIds", goods_recIds)
			.executeUpdate();
		return updatedEntities;
	}
	@Override
	public Integer addNewGoodsRecommended(IdxNewGoodsRecommended newGoods) {
		Session session = getSession();
		session.save(newGoods);
		return newGoods.getId();
	}
	@Override
	public void updateNewGoodsRecommended(IdxNewGoodsRecommended newGoods) {
		Session session = getSession();
		session.update(newGoods);
	}
	

}
